package com.jams.goodox.activity.qa;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.lfadapter.CommonRecycleViewAdapter;
import com.jams.goodox.adapter.lfadapter.LFRecyclerViewHolder;
import com.jams.goodox.entity.SeriousCourse;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import me.leefeng.lfrecyclerview.OnItemClickListener;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author HP
 */
public class AllCourseVideoActivity extends BaseActivity  implements OnItemClickListener,
        LFRecyclerView.LFRecyclerViewListener {

    private static final String TAG = "AllCourseVideoActivity";
    @Bind(R.id.recyclerView)
    LFRecyclerView recyclerView;

    private CommonRecycleViewAdapter pAdapter;

    private List<SeriousCourse> dataList = new ArrayList<>();
    private int seriesId;
    private int courseId;//
    private Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_allcourse_layout;
    }

    @Override
    protected void onResume() {
        super.onResume();
        dataList.clear();
        questionSeries();
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "全部系列");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        seriesId = getIntent().getIntExtra("seriesId",1);
        courseId = getIntent().getIntExtra("courseId",0);

        recyclerView.setFootText("---我是有底线的---");

        recyclerView.setLoadMore(false);
        recyclerView.setRefresh(false);
        recyclerView.setNoDateShow();
        recyclerView.setAutoLoadMore(false);
        recyclerView.setOnItemClickListener(this);
        recyclerView.setLFRecyclerViewListener(this);
        //recyclerView.setScrollChangeListener(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        pAdapter = new CommonRecycleViewAdapter(recyclerView,dataList,R.layout.push_layout) {
            @Override
            public void bindConvert(LFRecyclerViewHolder holder, int position, Object item, boolean isScrolling) {

                SeriousCourse seriousCourse = (SeriousCourse) item;
//                holder.setText(R.id.nickName,seriousCourse.getTitle());
//                holder.setText(R.id.time,seriousCourse.getContent());
//                holder.setText(R.id.see_number,seriousCourse.getClickNum() + "");
//                holder.setText(R.id.price,"" + seriousCourse.getPrice());

                //---------------
                Log.d(TAG,"seriousCourse = " + seriousCourse.toString());
                holder.setText(R.id.nickName,seriousCourse.getTitle());
                holder.setText(R.id.time, Utils.formatDateTime(seriousCourse.getCreateTime()));
                holder.setText(R.id.see_number,seriousCourse.getClickNum()+"");
                if(seriousCourse.getPrice() == 0){
                    holder. setVisible(R.id.money,false);
                }
                holder.setText(R.id.money,seriousCourse.getPrice() + "氧币");

            }

        };

        recyclerView.setAdapter(pAdapter);
    }


    private void questionSeries() {
        clearMap();
        params.put("requestData", "{seriesId:"  + seriesId +  "}");

        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);

        showLoading();

        createCallService(LoginService.class).questionSeries(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                misLoading();
                try {
                    String data = response.body().string();
                    Log.d(TAG, "questionSeries data = " + data);
                    JSONObject jsonObject = new JSONObject(data);

                    if (200 == jsonObject.getInt("code")) {
                        JSONArray object = new JSONArray(jsonObject.getString("responseData"));
                        Log.d(TAG, "responseData = " + object.toString());

                        if (null != object && object.length() > 0) {
                            for (int i = 0; i < object.length(); i++) {
                                JSONObject childObj = object.getJSONObject(i);
                                SeriousCourse freeCourse = new SeriousCourse();
                                //freeCourse.setId(childObj.getInt("id"));
                                freeCourse.setRadioLink(childObj.getString("radioLink"));
                                freeCourse.setDescr(childObj.getString("descr"));
                                freeCourse.setTitle(childObj.getString("title"));
                                freeCourse.setNickName(childObj.getString("nickName"));
                                freeCourse.setPrice(childObj.getInt("price"));
                                freeCourse.setCourseId(childObj.getInt("courseId"));
                                freeCourse.setImgPath(childObj.getString("imgPath"));

                                freeCourse.setClickNum(childObj.getInt("clickNum"));
                                freeCourse.setCreateTime(childObj.getLong("createTime"));
                                //freeCourse.setBuy(childObj.getBoolean("isBuy"));
                                freeCourse.setTranslateState(childObj.getInt("translateState"));

                                if(freeCourse.getCourseId() != courseId){
                                    dataList.add(freeCourse);
                                }
                            }
                        }

                        pAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                } catch (Exception e) {
                    ToastUtil.show("Exception:" + e.getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                ToastUtil.show("failure:" + t.getMessage());
            }
        });
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onClick(int position) {
        intent = new Intent(this, ClassLearningActivity.class);
        intent.putExtra("courseId", dataList.get(position).getCourseId());
        intent.putExtra("imagePath", dataList.get(position).getImgPath());
        intent.putExtra("nickName", dataList.get(position).getNickName());
        intent.putExtra("isFromSerious",true);
        if(dataList.get(position).getPrice() <= 0){
            intent.putExtra("canShare",true);
        }else {
            intent.putExtra("canShare",false);
        }
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onLongClick(int po) {

    }
}
